package com.sfeir.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.sfeir.client.i18n.TetrisConstants;
import com.sfeir.client.service.AppService;
import com.sfeir.client.service.AppServiceAsync;

public class ConnectedUserViewImpl extends Composite implements ConnectedUserView {

	private static ConnectedUserViewImplUiBinder uiBinder = GWT.create(ConnectedUserViewImplUiBinder.class);

	interface ConnectedUserViewImplUiBinder extends UiBinder<Widget, ConnectedUserViewImpl> {
	}

	@UiField Label label;
	@UiField HTML logout;

	public ConnectedUserViewImpl() {
		final TetrisConstants texts = GWT.create(TetrisConstants.class);
		AppServiceAsync service = GWT.create(AppService.class);
		service.getLogoutUrl(new AsyncCallback<String>() {
			@Override
			public void onSuccess(String result) {
				logout.setHTML("<a href=\"" + result + "\">" + texts.logout() + "</a>");
			}
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Unable to create logout url : " + caught.getMessage());
			}
		});
		initWidget(uiBinder.createAndBindUi(this));
	}

	public String getText() {
		return this.label.getText();
	}

	public void setText(String text) {
		this.label.setText(text);
	}
}
